package com.jijidom.order.service;

import com.jijidom.order.client.ProductClient;
import com.jijidom.order.dto.CarDTO;
import com.jijidom.order.dto.OrderDTO;
import com.jijidom.order.entity.OrderDetail;
import com.jijidom.order.entity.OrderMaster;
import com.jijidom.order.entity.ProductInfo;
import com.jijidom.order.enums.OrderStatusEnum;
import com.jijidom.order.enums.PayStatusEnum;
import com.jijidom.order.repository.OrderDetailRepository;
import com.jijidom.order.repository.OrderMasterRepository;
import com.jijidom.order.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName
 * @Description TODO
 * @Author JinTao
 * @Date 2018/9/11 15:30
 * @Version 1.0
 **/
@Service
public class OrderService {

    @Autowired
    private OrderMasterRepository orderMasterRepository;

    @Autowired
    private OrderDetailRepository orderDetailRepository;

    @Autowired
    private ProductClient productClient;

    /**
     * 创建订单
     * @param orderDTO
     */
    public OrderDTO create(OrderDTO orderDTO){
        String OrderId = KeyUtil.genUniqueKey();
        //TODO 查询商品信息(调用商品服务)
        List<String> productIdList = orderDTO.getOrderDetails().stream().map(OrderDetail::getProductId).collect(Collectors.toList());
        List<ProductInfo> productInfoList = productClient.listForOrder(productIdList);
        //TODO 计算总价
        BigDecimal orderAmout = new BigDecimal(BigInteger.ZERO);
        for (OrderDetail orderDetail: orderDTO.getOrderDetails()){
            for (ProductInfo productInfo: productInfoList){
                if (productInfo.getProductId().equals(orderDetail.getProductId())){
                    //单价*数量
                    orderAmout = productInfo.getProductPrice().multiply(new BigDecimal( orderDetail.getProductQuantity())).add(orderAmout);
                    BeanUtils.copyProperties(productInfo,orderDetail);
                    orderDetail.setOrderId(OrderId);
                    orderDetail.setDetailId(KeyUtil.genUniqueKey());
                    orderDetailRepository.save(orderDetail);
                }
            }
        }
        //TODO 扣库存
        List<CarDTO> carDTOList = orderDTO.getOrderDetails().stream()
                .map(e -> new CarDTO(e.getProductId(),e.getProductQuantity()))
                .collect(Collectors.toList());
        productClient.decreaseStock(carDTOList);
        /**
         * 订单入库
         */
        OrderMaster orderMaster = new OrderMaster();
        orderDTO.setOrderId(OrderId);
        BeanUtils.copyProperties(orderDTO,orderMaster);
        orderMaster.setOrderAmount(orderAmout);
        orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode());
        orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode());
        orderMasterRepository.save(orderMaster);
        return orderDTO;
    }
}
